package com.sdtele.tta.operation;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.sdtele.tta.dac.Dac;
import com.sdtele.tta.models.JsonRequest;
import com.sdtele.tta.models.JsonResponse;
import com.sdtele.tta.models.User;
import com.sdtele.tta.utils.TeamUtils;

/**
 * 操作类实现：GetUser(json.operation=getUser)
 * 
 * @author XCream
 */
public class OperationAddUser extends Operation {

	@Override
	public JsonResponse handle(JsonRequest jsonRequest) {
		JsonResponse jres = new JsonResponse();
		User curUser = (User) request.getSession().getAttribute("user");
		if (curUser!=null && curUser.getIsManager() == 1) {
		
			String account = jsonRequest.getJsonObject().get("account").getAsString();
			if (account.isEmpty()) {
				jres.setStatus(1);
				jres.setMessage("账号不能为空");
				return jres;
			}
			if (Dac.getInstance().accoutExist(account)) {
				jres.setStatus(1);
				jres.setMessage("账号已存在");
				return jres;
			}
			String passwd = jsonRequest.getJsonObject().get("passwd").getAsString();
			if (passwd.isEmpty()) {
				jres.setStatus(1);
				jres.setMessage("密码不能为空");
				return jres;
			}
			String name = jsonRequest.getJsonObject().get("name").getAsString();
			if (name.isEmpty()) {
				jres.setStatus(1);
				jres.setMessage("姓名不能为空");
				return jres;
			}
			Timestamp createTime = new Timestamp(new java.util.Date().getTime());
			Long teamId = jsonRequest.getJsonObject().get("teamId").getAsLong();
			if (teamId.equals(null)) {
				jres.setStatus(1);
				jres.setMessage("组织机构不能为空");
				return jres;
			}
			Set<Long> teamIdSet = new HashSet<Long>();
			teamIdSet.add(curUser.getTeamId());
			TeamUtils.teamAuthority(curUser.getTeamId(), teamIdSet);
			if (!teamIdSet.contains(teamId)) {
				jres.setStatus(1);
				jres.setMessage("您的管理权限不够加该组织下员工");
				return jres;
			}
			int isManager = jsonRequest.getJsonObject().get("isManager").getAsInt();
			String phone = jsonRequest.getJsonObject().get("phone").getAsString();
			String email = jsonRequest.getJsonObject().get("email").getAsString();
			String sex = jsonRequest.getJsonObject().get("sex").getAsString();
			String avatar = "/file/men.jpeg";
			User user = new User();
			user.setAccount(account);
			user.setPasswd(passwd);
			user.setName(name);
			user.setCreateTime(createTime);
			user.setTeamId(teamId);
			user.setIsManager(isManager);
			user.setPasswd(passwd);
			user.setEmail(email);
			user.setPhone(phone);
			user.setSex(sex);
			if(user.getSex().equals("女"))
			{avatar="/file/female.jpeg";}
			user.setAvatar(avatar);
			Dac.getInstance().addUser(user);
			if (user.getId() > 0) {
				jres.setStatus(0);
				jres.setMessage("添加用户成功");
				jres.setEntity(user);
			} else {
				jres.setStatus(1);
				jres.setMessage("未知错误，添加用户失败");
			}
		} else {
			jres.setStatus(1);
			jres.setMessage("您不是管理员，不能添加用户");
		}
		return jres;
	}

}
